<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品促销操作功能
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	allen.qiang
 */

quanli_require('module/base/common/quanliCommon.class.php');
quanli_require('module/base/system/sysPayment.class.php');
quanli_require('module/base/customer/custRank.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/promotion/quanliCard.class.php');
quanli_require('module/base/activity/activityConfig.class.php');
class pa_promotionGoods {
    public function execute() {
		global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品促销列表
                    $this->promotionGoodsList();
                    break;
                case 100: //商品促销添加
                    $this->promotionGoodsAdd();
                    break;
                case 101: //商品促销编辑
                    $this->promotionGoodsEdit();
                    break;
                case 102: //商品促销配置显示 add by allen.qiang 2015-05-03
                    $this->promotionGoodsConfig();
                    break;
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品促销列表
     * @author kangronghua
     * 
     */
    private function promotionGoodsList() {
        global $G_PGLOB;

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(4504));

        $G_PGLOB->smarty->display('promotion/promotionGoods_list.tpl');
    }
    
    /**
     * 商品促销添加 
     *
     */
    private function promotionGoodsAdd() {
    	global $G_PGLOB;
    
    	//得到最大的商品促销排序
    	$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT seqno+1 AS max_sortOrder FROM promotion_seqno') ;

    	$promotionGoodsInfo = new entity('goods_spec_group') ;
    	$promotionGoodsInfo->sortOrder = $maxSortOrder[0]->max_sortOrder ;
    	$promotionGoodsInfo->useFlg    = 1 ;


    	//商品筛选器
        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
    	$arg_array = array(
			'sortname' 	=> 'selectID' , 
			'sortorder' => 'DESC' , 
			'page' 		=> 0 , 
			'rp' 		=> 0 
		);
		
		$result_data = $commHandler->search($arg_array , 'goods_selector') ;
		$promotionGoodsInfo->goodsSelector= $result_data ;

        //获取支付方式
        $sysPayment = new sysPayment($G_PGLOB->DBCA );
        $arg_array = array(
            'useFlg' => 1,
            'extend' => ' AND type = 1 OR type = 2'
        );
        $pay_type_array = $sysPayment->searchPayType($arg_array);
        $G_PGLOB->smarty->assign_by_ref('pay_type_array' , $pay_type_array);

        //代金卡面值
        $quanliCard = new quanliCard($G_PGLOB->DBCA);
        $card_array = $quanliCard->searchCardFaceValue();
        $G_PGLOB->smarty->assign_by_ref('card_array',$card_array);


        //获取会员等级
        $custRank = new custRank($G_PGLOB->DBCA);
        $arg_array = array(
            'useFlg' => 1,
        );
        $cust_rank_array = $custRank->searchRank($arg_array);
        $G_PGLOB->smarty->assign_by_ref('cust_rank_array' , $cust_rank_array);

        //获取促销渠道信息
        $itemHandler = new itemHandler($G_PGLOB->DBCA);
        $arg_array = array(
            'itemCategoryID'=>1,
            'useFlg' => 1
        );
        $promotion_channel_array = $itemHandler->searchItem($arg_array);
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array' , $promotion_channel_array);

        //获取城市信息
        $city_array = get_city();
        $G_PGLOB->smarty->assign_by_ref('city_array', $city_array);

    	$G_PGLOB->smarty->assign_by_ref('promotionGoodsInfo' , $promotionGoodsInfo);
    	$G_PGLOB->smarty->display('promotion/promotionGoods_add.tpl');
    }
    
    /**
     * 商品促销编辑 
     *
     */
    private function promotionGoodsEdit() {
    	global $G_PGLOB;
    	
    	//得到商品促销信息
    	$promotionGoodsID   = $_REQUEST['promotionID'] ;
    	$promotionGoodsInfo = new entity('promotion_goods') ;
    	$promotionGoodsInfo->promotionID = $promotionGoodsID ;
    	if(!$G_PGLOB->DBCA->fetch( $promotionGoodsInfo )){
    		$G_PGLOB->smarty->assign('error_msg', sprintf('商品促销号「%d」,的商品促销不存在，请审核后再操作',$promotionGoodsID));
			$G_PGLOB->smarty->display('comm/error.tpl');
			die() ;
    	}
    	
    	//促销商品详情
    	$goods_detail = $G_PGLOB->DBCA->stmt_query('SELECT * FROM promotion_goods_detail WHERE promotionID=?' ,'i' ,$promotionGoodsID) ; 
    	$promotionGoodsInfo->goods_detail = $goods_detail;
    	
 
    	//商品筛选器
        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
    	$arg_array = array(
			'sortname' 	=> 'selectID' , 
			'sortorder' => 'DESC' , 
			'page' 		=> 0 , 
			'rp' 		=> 0 
		);
		
		$result_data = $commHandler->search($arg_array , 'goods_selector') ;
		$promotionGoodsInfo->goodsSelector= $result_data ;

        //获取支付方式
        $sysPayment = new sysPayment();
        $arg_array = array(
            'useFlg' => 1,
            'extend' => ' AND type = 1 OR type = 2'
        );
        $pay_type_array = $sysPayment->searchPayType($arg_array);
        $G_PGLOB->smarty->assign_by_ref('pay_type_array' , $pay_type_array);

        //获取选择了的付款方式
        $arg_array = array(
            'promotionID' => $promotionGoodsID,
            'type' => 1
        );
        $result = $sysPayment->searchPromotionPayment($arg_array);

        $select_paymentID = array();
        if($result){
            foreach($result as &$v){
                $select_paymentID[] = $v['paymentID'];
            }
        }
        $G_PGLOB->smarty->assign_by_ref('select_paymentID' , $select_paymentID);

        $promotionGoodsInfo->payTypeID = explode(",",$promotionGoodsInfo->payTypeID);

        //获取所选支付方式下的付款方式
        $payment_array = array();
        if($promotionGoodsInfo->payTypeID){
            foreach($promotionGoodsInfo->payTypeID as $payType){

                $arg_array = array(
                    'payTypeID' => $payType
                );
                $result = $sysPayment->searchPaymentMethod( $arg_array );
                if(!$result){
                    continue;
                }

                $payment_array = array_merge($payment_array,$result);

            }
        }

        if(empty($payment_array)){
            $payment_array = null;
        }
        $G_PGLOB->smarty->assign_by_ref('payment_array' , $payment_array);

        //获取会员等级
        $custRank = new custRank($G_PGLOB->DBCA);
        $arg_array = array(
            'useFlg' => 1
        );
        $cust_rank_array = $custRank->searchRank($arg_array);
        $G_PGLOB->smarty->assign_by_ref('cust_rank_array' , $cust_rank_array);

        //获取促销渠道信息
        $itemHandler = new itemHandler($G_PGLOB->DBCA);
        $arg_array = array(
            'itemCategoryID'=>1,
            'useFlg' => 1
        );
        $promotion_channel_array = $itemHandler->searchItem($arg_array);
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array' , $promotion_channel_array);

        //代金卡面值
        $quanliCard = new quanliCard($G_PGLOB->DBCA);
        $card_array = $quanliCard->searchCardFaceValue();
        $G_PGLOB->smarty->assign_by_ref('card_array',$card_array);


        //查询城市信息
        $city_array = get_city();
        $G_PGLOB->smarty->assign_by_ref('city_array', $city_array);

        $promotionGoodsInfo->cityID = explode(",",$promotionGoodsInfo->cityID);
        $promotionGoodsInfo->rankID = explode(",",$promotionGoodsInfo->rankID);
        $promotionGoodsInfo->use_card_face_value = explode(",",$promotionGoodsInfo->use_card_face_value);
        $promotionGoodsInfo->promotionChannelID = explode(",",$promotionGoodsInfo->promotionChannelID);

    	$G_PGLOB->smarty->assign('promotionInfo' , $promotionGoodsInfo);
    	$G_PGLOB->smarty->display('promotion/promotionGoods_edit.tpl');
    }

    /**
     * 商品促销配置
     * add by allen.qiang 2015-05-03
     */
    private function promotionGoodsConfig() {
        global $G_PGLOB;

        //得到商品促销信息
        $promotionID   = $_REQUEST['promotionID'] ;
        $promotionInfo = new entity('promotion_goods') ;
        $promotionInfo->promotionID = $promotionID ;
        if(!$G_PGLOB->DBCA->fetch( $promotionInfo )){
            $G_PGLOB->smarty->assign('error_msg', sprintf('商品促销号「%d」,的商品促销不存在，请审核后再操作',$promotionID));
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        if($promotionInfo->promotionChannelID){

            //活动配置详情
            $activityConfigInfo = $G_PGLOB->DBCA->stmt_query("SELECT config_code FROM activity_config WHERE config_key = 'billPromotion' AND config_value = ?", 'i' ,$promotionInfo->promotionChannelID) ;

            if($activityConfigInfo){
                $config_code = $activityConfigInfo[0]->config_code;
            }else{
                $config_code = '';
            }

        }else{
            $config_code = '';
        }

        //实例化活动配置类
        $systemConfig = $G_PGLOB->DBCA->stmt_query("SELECT config_code,config_key,config_value,config_remark FROM activity_config WHERE config_code = ? ORDER BY config_id", 's' ,'system');

        if($config_code != ''){
            $activityConfig = new activityConfig($config_code);
            $config = $activityConfig->config;
        }else{
            $config = '';
        }

        if($config){
            $config['payTypeID'] = explode(",",$config['payTypeID']);
            $config['paymentID'] = explode(",",$config['paymentID']);
        }else{
            $config['payTypeID'] = $config['paymentID'] = array();
        }

        //获取支付方式
        $sysPayment = new sysPayment();
        $arg_array = array(
            'useFlg' => 1,
            'extend' => ' AND type = 1 OR type = 2'
        );
        $pay_type_array = $sysPayment->searchPayType($arg_array);
        $G_PGLOB->smarty->assign_by_ref('pay_type_array' , $pay_type_array);

        //获取选择了的付款方式
        $select_paymentID = array_unique(array_merge(explode(",",$promotionInfo->paymentID),$config['paymentID']));

        $G_PGLOB->smarty->assign_by_ref('select_paymentID' , $select_paymentID);

        //获取选择了的支付方式
        $promotionInfo->payTypeID = array_unique(array_merge(explode(",",$promotionInfo->payTypeID),$config['payTypeID']));

        //获取所选支付方式下的付款方式
        $payment_array = array();
        if($promotionInfo->payTypeID){
            foreach($promotionInfo->payTypeID as $payType){

                $arg_array = array(
                    'payTypeID' => $payType
                );
                $result = $sysPayment->searchPaymentMethod( $arg_array );
                if(!$result){
                    continue;
                }

                $payment_array = array_merge($payment_array,$result);

            }
        }

        if(empty($payment_array)){
            $payment_array = null;
        }
        $G_PGLOB->smarty->assign_by_ref('payment_array' , $payment_array);

        //获取促销渠道信息
        $itemHandler = new itemHandler($G_PGLOB->DBCA);
        $arg_array = array(
            'itemCategoryID'=>1,
            'useFlg' => 1
        );
        $promotion_channel_array = $itemHandler->searchItem($arg_array);
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array' , $promotion_channel_array);

        $promotionInfo->promotionChannelID = explode(",",$promotionInfo->promotionChannelID);

        $G_PGLOB->smarty->assign('config_code' , $config_code);
        $G_PGLOB->smarty->assign('systemConfig' , $systemConfig);
        $G_PGLOB->smarty->assign('activityConfig' , $config);
        $G_PGLOB->smarty->assign('promotionInfo' , $promotionInfo);
        $G_PGLOB->smarty->display('promotion/promotionGoods_config.tpl');
    }
}